package com.ewzj.bigdata.mvp.presenter;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.model.api.Api;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqFilterBean;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqPageBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BannerBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.FilterBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.CommonContract;

import java.util.List;


@ActivityScope
public class CommonPresenter extends BasePresenter<CommonContract.Model, CommonContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private Gson mGson;
    private int page=1;
    @Inject
    public CommonPresenter(CommonContract.Model model, CommonContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager,Gson gson) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mGson=gson;
        this.mAppManager = appManager;
    }
    public void getInitData(List<NewBean> newBeans){
        ReqPageBean reqPageBean=new ReqPageBean(page, AppConstants.PAGE_NUM,mRootView.obtainCurrentTab().getId());
        mModel.getnews(Apputils.json2Body(mGson.toJson(reqPageBean))).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<NewBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<NewBean> newBeans2) {
//                        newBeans.clear();
//                        newBeans.addAll(newBeans2);
                        page++;
                        newBeans.clear();
                        newBeans.addAll(newBeans2);
                        mRootView.refreshNewsChange(newBeans);
                    }
                });
    }
    public void loadmoredata() {
       ReqPageBean reqPageBean=new ReqPageBean(page,AppConstants.PAGE_NUM,mRootView.obtainCurrentTab().getId());
       mModel.getnews(Apputils.json2Body(mGson.toJson(reqPageBean))).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(newBeans -> {
                    mRootView.notifyNewsAdd(newBeans);
                    mRootView.obtainAdapter().loadMoreComplete();
                    if (newBeans.size()<AppConstants.PAGE_NUM){
//                            数据小于一页的数据, 数据加载完毕
                        mRootView.obtainAdapter().loadMoreEnd();
                    }
                    page++;

                },throwable -> {
                    mRootView.obtainAdapter().loadMoreFail();
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void getbannerData(String id) {
        mModel.getBanners(id).compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<BannerBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<BannerBean> bannerBeans) {
                        mRootView.loadBannerView(bannerBeans);
                    }
                });
    }


    public void refrehdata() {
        page=1;
        ReqPageBean reqPageBean=new ReqPageBean(page, AppConstants.PAGE_NUM,mRootView.obtainCurrentTab().getId());
        mModel.getnews(Apputils.json2Body(mGson.toJson(reqPageBean))).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<NewBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<NewBean> newBeans2) {
                        page++;
                        mRootView.refreshNewsChange(newBeans2);
                    }
                });
    }

    public void loadFilterData(String id) {
        mModel.getFilterdatas(id)
                .compose(RxUtils.applySchedulersWithoutLoading(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<FilterBean>>(mErrorHandler) {
                    @Override
                    public void onNext(List<FilterBean> filterBeans) {
                        mRootView.popwindowShow(filterBeans);
                    }
                });
    }


}
